<?php

ini_set('date.timezone','Asia/Shanghai');
$arr = input();
if(!$arr){ exit('非法访问');}
$vString = "swt".$arr['code'].$arr['mac'].date("ymdhis")."swt";
$key = strtoupper(dechex(crc32($vString)));
$date=date("ymdhis");
if($arr['code']==31) {
    $data = array(
        "code" =>"31",
        "mac" => $arr['mac'],
        "time" =>$date,
        "key" => $key,
    );
    echo json_encode($data);
    exit;
}
if(input()) {
    $arr = input();
    $_GET;
    // $get = explode("=", $arr['cid']);
    $cid=$arr['cid'];
    $type= substr($cid,0,-(strlen($cid)-1));
    //  $type = $arr['type'];//订单类型
    $id =  substr($cid,0,1);//订单id
    if($type==4){ //4为会员二维码
        $member = checkMember($id);//查询会员表
        if(!$member){
            $where['o.id'] = $id;
            $message = "------无效的二维码------";
            $relay = '0000';
            $ordersn = '';
            $beep = 21;
            goto a;
        }
        //1为银卡会员2金卡会员3铂金卡会员4黑金卡会员
        switch($member['type']){
            case 1:
                $message = "------会员等级不够------";
                $relay = '0000';
                $ordersn = '';
                $beep = 21;
                break;
            case 2:
                $message = "------金卡会员------";
                $relay = '1000';
                $ordersn = '';
                $beep = 11;
                break;
            case 3:
                $message = "------铂金卡会员------";
                $relay = '1000';
                $ordersn = '';
                $beep = 11;
                break;
            case 4:
                $message = "------黑金卡会员------";
                $relay = '1000';
                $ordersn = '';
                $beep = 11;
                break;
            default:
                $message = "------会员等级不够------";
                $relay = '0000';
                $ordersn = '';
                $beep = 21;
                break;
        }

    }
    if ($type != 4) {//4为会员二维码
        //游泳馆设备号
        $you = db("config")->field("value")->where("id='2'")->find();
        $swim = explode(",", $you['value']);
        $jian = db("config")->field("value")->where("id='3'")->find();
        $strong = explode(",", $jian = $jian['value']);
        $allmac = array_merge($swim, $strong);
        $db_order = db('order');
        $where['o.status'] = 1;
        $where['o.delete'] = 0;
        $where['o.id'] = $id;
        $message = "------无效的二维码------";
        $relay = '0000';
        $ordersn = '0';
        $beep = 21;
        if (!in_array($arr['mac'], $swim)) {
            $message = "------系统暂无该设备号------";
        }
        if ($type == 0 || $type == 1) {//房间或者门票
            $order = $db_order->alias("o")
                ->field("h.type as htype,o.*")
                ->join("hotel_room h", "h.id=o.goodsid")
                ->where($where)
                ->find();
            if (!$order) {
                $message = "------无效的二维码------";
                $relay = '0000';
                $ordersn ='';
                $beep = 21;
            }
            if ($order['htype'] == 1 && in_array($arr['mac'], $swim)) {  //票的类型是游泳馆,并且这台机器是扫描游泳馆
                if ($order['start_time'] != strtotime(date("Y-m-d"))) {
                    $message = "------已过期------";
                    $relay = '0000';
                    $ordersn ='订单号：'.$order['order_sn'];
                    $beep = 21;
                } else {
                    if ($order['usecount'] >= $order['count']) {
                        $message = "------次数已用光------";
                        $relay = '0000';
                        $ordersn ='订单号：'.$order['order_sn'];
                        $beep = 21;
                    } else {
                        $add = $db_order->alias("o")
                            ->field("h.type as htype,o.*")
                            ->join("hotel_room h", "h.id=o.goodsid")
                            ->where($where)
                            ->setInc("usecount");
                        $res = $db_order->alias("o")
                            ->field("h.type as htype,o.*")
                            ->join("hotel_room h", "h.id=o.goodsid")
                            ->where($where)
                            ->update(array("o.status" => 2));
                        $message = "------扫描成功------";
                        $relay = '1000';
                        $ordersn ='订单号：'.$order['order_sn'];
                        $beep = 11;
                    }
                }
            }
            if ($order['htype'] == 2 && in_array($arr['mac'], $strong)) { //票的类型是健身房,并且这台机器是扫描健身房
                if ($order['start_time'] != strtotime(date("Y-m-d"))) {
                    $message = "------已过期------";
                    $relay = '1000';
                    $ordersn ='订单号：'.$order['order_sn'];
                    $beep = 21;
                } else {
                    if ($order['usecount'] >= $order['count']) {
                        $message = "------次数已用光------";
                        $relay = '1000';
                        $ordersn ='订单号：'.$order['order_sn'];
                        $beep = 21;
                    } else {
                        $add = $db_order->alias("o")
                            ->field("h.type as htype,o.*")
                            ->join("hotel_room h", "h.id=o.goodsid")
                            ->where($where)
                            ->setInc("usecount");
                        $res = $db_order->alias("o")
                            ->field("h.type as htype,o.*")
                            ->join("hotel_room h", "h.id=o.goodsid")
                            ->where($where)
                            ->update(array("o.status" => 2));
                        $message = "------扫描成功------";
                        $relay = '1000';
                        $ordersn ='订单号：'.$order['order_sn'];
                        $beep = 11;
                    }
                }
            }
            if ($order['htype'] == 0 && in_array($arr['mac'], $allmac)) {  //如果是房间并且是系统设置的设备号
                if ($order['end_time'] <= strtotime(date("Y-m-d"))) {
                    $message = "------已过期------";
                    $relay = '0000';
                    $ordersn ='订单号：'.$order['order_sn'];
                    $beep = 21;
                } else {
                    $add = $db_order->alias("o")
                        ->field("h.type as htype,o.*")
                        ->join("hotel_room h", "h.id=o.goodsid")
                        ->where($where)
                        ->setInc("usecount");
                    $message = "------扫描成功------";
                    $relay = '1000';
                    $beep = 11;
                }
            }
        }
        if ($type == 2) {//套票
            $order = $db_order->alias("o")
                ->field("o.*")
                ->where($where)
                ->find();
            if ($order['end_time'] <= strtotime(date("Y-m-d"))) {
                $message = "------已过期------";
                $relay = '1000';
                $ordersn ='订单号：'.$order['order_sn'];
                $beep = 21;
            }
            if ($order['isroom'] == 0 && in_array($arr['mac'], $allmac)) {       //房间套票
                $add = $db_order->alias("o")
                    ->field("h.type as htype,o.*")
                    ->join("hotel_room h", "h.id=o.goodsid")
                    ->where($where)
                    ->setInc("usecount");
                $message = "------扫描成功------";
                $relay = '1000';
                $beep = 11;
            }
            if ($order['isroom'] == 1) {       //门票套票
                $o_pack = db("order_package")->alias("p")
                    ->field("r.type,p.*")
                    ->join("hotel_room r", "r.id=p.goodsid")
                    ->where("p.order_id=" . $order['id'])
                    ->select();
                foreach ($o_pack as $p) {
                    if ($p['type'] == 1 && in_array($arr['mac'], $swim)) {      //游泳馆
                        if ($p['num'] > $p['usenum']) {
                            db("order_package")
                                ->where("id=" . $p['id'])
                                ->setInc("usenum");
                            $message = "------扫描成功-----";
                            $relay = '1000';
                            $ordersn ='订单号：'.$order['order_sn'];
                            $beep = 11;
                        } else {
                            $message = "------次数已用完1------";
                            $relay = '0000';
                            $ordersn ='订单号：'.$order['order_sn'];
                            $beep = 21;
                        }
                    }
                    if ($p['type'] == 2 && in_array($arr['mac'], $strong)) {      //健身房
                        if ($p['num'] > $p['usenum']) {
                            db("order_package")
                                ->where("id=" . $p['id'])
                                ->setInc("usenum");
                            $message = "------扫描成功-----";
                            $relay = '1000';
                            $ordersn ='订单号：'.$order['order_sn'];
                            $beep = 11;
                        } else {
                            $message = "------次数已用完2------";
                            $relay = '0000';
                            $ordersn ='订单号：'.$order['order_sn'];
                            $beep = 21;
                        }
                    }
                }
            }
        }
        //  file_put_contents("aaa.txt",$arr,FILE_APPEND);
    }
}

a:  $data=array(
    "code"=>$arr['code'],
    "mac"=>$arr['mac'],
    "time"=>$date,
    "key"=>$key,
    "beep"=>$beep,
    "relay"=>$relay,
    "relaytype"=>"0",
    "imgid"=>"0",
    "line1c"=>'FFFF',
    "line1t"=>'',
    "line2c"=>"07EO",
    "line2t"=>$message,
    "line3c"=>"07EO",
    "line3t"=>$ordersn,
    "line4c"=>"07EO",
    "line4t"=>"",
    "line5c"=>"07EO",
    "line5t"=>"",
    "line6c"=>"07EO",
    "line6t"=>"",
    "sptxt"=>"测试数据",
);
return json_encode($data);